package com.hsy.bibc.modules.order.frament;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.ui.widget.DPCircleImageView;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsy.bibc.R;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.modules.common.CommonBaseFragment;
import com.hsy.bibc.modules.common.PullableListView;
import com.hsy.bibc.modules.order.gsondata.OrderListGsonData;
import com.hsy.bibc.modules.order.OrderDetailActivity;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.utils.HttpUtil;
import com.hsy.bibc.utils.VolleyLoadPicture;
import com.jingchen.pulltorefresh.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 123 on 2016/1/21.
 */
public class FragmentExChangeList extends CommonBaseFragment {

    private AdapterMyThing adapterFragmentInformation;
    private PullableListView list_view;
    private List<OrderListGsonData.Data> listData = new ArrayList<>();
    private LayoutInflater inflater;


    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {

            if (0 == what) {
                OrderListGsonData.Result result = DPGsonUtil.fromJson(response, OrderListGsonData.Result.class);
                if (result == null) {
                    showToast("服务器内部错误!");
                    CommonUtils.hideProgressDialog();
                    return;
                }
                showUI(result);
                CommonUtils.hideProgressDialog();
                getRootView().setVisibility(View.VISIBLE);
            } else if (1 == what) {
                listData.clear();
                OrderListGsonData.Result result = DPGsonUtil.fromJson(response, OrderListGsonData.Result.class);
                showUI(result);

                list_view.refreshFinish(PullableListView.REFRESH_FINISH);
            } else if (2 == what) {

                OrderListGsonData.Result result = DPGsonUtil.fromJson(response, OrderListGsonData.Result.class);

                nextPage = result.getNextPage();

                listData.addAll(result.getData());

                adapterFragmentInformation.notifyDataSetChanged();

                list_view.loadmoreFinish(PullableListView.LOAD_FINISH);

            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
            if (1 == what) {
                list_view.refreshFinish(PullableListView.FAIL);

            } else if (2 == what) {
                list_view.loadmoreFinish(PullableListView.FAIL);

            }
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            CommonUtils.hideProgressDialog();
            if (1 == what) {
                list_view.refreshFinish(PullableListView.FAIL);

            } else if (2 == what) {
                list_view.loadmoreFinish(PullableListView.FAIL);

            }
        }
    };
    private String nextPage;
    private View empty_linear;

    private void showUI(OrderListGsonData.Result result) {
        nextPage = result.getNextPage();
        listData = result.getData();
        adapterFragmentInformation = new AdapterMyThing();
        list_view.setAdapter(adapterFragmentInformation);
        if ("0".equals(result.getCount())) {

            empty_linear.setVisibility(View.VISIBLE);

            list_view.setVisibility(View.GONE);
        } else {

            empty_linear.setVisibility(View.GONE);

            list_view.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_exchange;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        inflater = LayoutInflater.from(getAttchedActivity());
        list_view = F(R.id.exchange_list);
        empty_linear = findViewById(R.id.stub_import);
        getRootView().setVisibility(View.GONE);

        list_view.getListView().setDivider(null);
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(OrderDetailActivity.class, listData.get(position).getOrder_id(), listData.get(position).getType());
            }
        });
        list_view.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {


                HttpUtil.httpGet(getAttchedActivity(), 1, Consts.API_GET_MY_COLLECT_HD + "?type=2", callback);

            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

                if ("0".equals(nextPage)) {
                    showToast(R.string.txt_no_more_data);
                    list_view.loadmoreFinish(PullableListView.LOAD_FINISH);
                    return;
                }
                HttpUtil.httpGet(getAttchedActivity(), 2, Consts.API_GET_MY_COLLECT_HD + "?type=2&page=" + nextPage, callback);
            }
        });
    }

    private void getHttp() {
        CommonUtils.showProgressDialog(getAttchedActivity());
        HttpUtil.httpGet(getAttchedActivity(), 0, Consts.API_GET_MY_COLLECT_HD + "?type=2", callback);
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void onHandleMessage(Message msg) {

    }

    @Override
    public void onFragmentUpdate() {
        getHttp();
    }

    private class AdapterMyThing extends BaseAdapter {

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            view = inflater.inflate(R.layout.item_order, null);

            TextView nick_name = (TextView) view.findViewById(R.id.nick_name);
            TextView view_num = (TextView) view.findViewById(R.id.order_number);
            TextView type_name = (TextView) view.findViewById(R.id.type_name);
            TextView create_datetime_str = (TextView) view.findViewById(R.id.create_datetime_str);
            DPCircleImageView avatar = (DPCircleImageView) view.findViewById(R.id.avatar);

//            DPPicassoUtil.with(getAttchedActivity()).load(listData.get(i).getUser().getAvatar()).into(avatar);
            VolleyLoadPicture vlp = new VolleyLoadPicture(getAttchedActivity(), avatar);
            vlp.getmImageLoader().get(listData.get(i).getUser().getAvatar(), vlp.getOne_listener());
            nick_name.setText(listData.get(i).getUser().getNickname());
            view_num.setText(listData.get(i).getOrder_sn());
            type_name.setText(listData.get(i).getType_name() + ":");
            create_datetime_str.setText(listData.get(i).getCreate_datetime_str());


            return view;
        }
    }
}
